package com.collectplus.express.tools;

import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

public class LengthInputFilter implements InputFilter {

	private int length;

	private EditText mEditText;

	private InputCallback callback;

	public interface InputCallback {
		public void onCallback(EditText editText);
	}

	public LengthInputFilter setInputCallback(InputCallback callback) {
		this.callback = callback;
		return this;
	}

	public LengthInputFilter(EditText editText, int length) {
		this.mEditText = editText;
		this.length = length;
	}

	/**
	 * 如果返回"" 表示EditText将过滤掉该字符
	 */
	@Override
	public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
		if (source == null || source.length() == 0) {
			return "";
		}
		if (callback != null) {
			callback.onCallback(this.mEditText);
		}
		if (mEditText.length() >= length) {
			return "";
		}
		return source;
	}

}
